<?php
namespace app\common\model;

use think\Model;
use think\Db;
use traits\model\SoftDelete;
use app\common\model\Trends;

class Circle extends Model
{
	use SoftDelete;
	// 设置当前模型使用的完整数据库连接信息
	protected $connection = 'database.db_second';//连接配置

    // 表名
    protected $name = 'circle';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
	    'logo_image_url',
	    'logo_time_text',
	    'image_url',
	    'image_time_text',
         'status_text',
        'join_state_text',
        'trends_count',
        'user_count'
    ];
    

    
    public function getStatusList()
    {
        return ['1' => __('审核中'), '2' => __('已通过'), '3' => __('已驳回')];
    }

    public function getJoinStateList()
    {
        return ['1' => __('直接加入'), '2' => __('点击加入'), '3' => __('申请加入'), '4' => __('答题加入')];
    }

    public function getTrendsCountAttr($value, $data){

    	return Trends::where(['circle_id'=>$data['id'],'is_draft'=>2,"is_see"=>1,"state"=>1])->count();

    }

	public function getUserCountAttr($value, $data){

		$count = Db::connect($this->connection)->name('circle_user')->where(['circle_id'=>$data['id'],'identity'=>array('in','1,2,3')])->count();
		return $count?$count:0;

	}

    public function getLogoTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['logo_time']) ? $data['logo_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getImageTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['image_time']) ? $data['image_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getJoinStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['join_state']) ? $data['join_state'] : '');
        $list = $this->getJoinStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setLogoTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setImageTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function cate()
    {
        return $this->belongsTo('app\common\model\circle\Cate', 'circlecate_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

	public function getImageUrlAttr($value, $data){

		$value = $value ?: $data["image"];
		return getImagesUrl($value);

	}

	public function getLogoImageUrlAttr($value, $data){

		$value = $value ?: $data["logo_image"];
		return getImagesUrl($value);

	}


}
